package com.tgpms.vforms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.vforms.entity.Person;
import com.tgpms.vforms.service.PersonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class PersonServiceImpl implements PersonService {

    @Resource
    private VformInterface vformInterface;

    @Override
    public Result selPersonList(String po_no,String division, String department, String entityItem, String company, String pageNo, String pageSize, String dataowner) throws Exception {
        Result<Object> result = new Result<Object>();
        JSONObject json = new JSONObject();
        json.put("[SQL1]", "");
        json.put("[SQL2]", "");
        String condition = "";
        if(!Common.isEmpty(po_no)) {
        	JSONObject json1 = new JSONObject();
        	json.put("PO_NO=", po_no);
            Result<Object> result1;
            if (StringUtils.isEmpty(dataowner))
                result1 = vformInterface.find("D4001", json1.toJSONString(), pageNo, pageSize,"N","N","");
        	else
                result1 = vformInterface.find("D4001", json1.toJSONString(), pageNo, pageSize,"Y","Y",dataowner);
        	List<Map<String,Object>> polist = JSONArray.parseObject(result1.getData().toString(), List.class);
        	Map<String,Object> map = polist.get(0);
        	if(polist!=null && polist.size()>0) {
        		condition=condition+" and (a.division = '"+map.get("DIVISION").toString()
        				+"' or a.division ='"+map.get("COMPANY").toString()
        				+"' or a.division ='"+map.get("COMPANY_SUPERVISION").toString()+"')";
        	} else {
        		return result;
        	}
        }
        if(StringUtils.isNotBlank(division)) {
            condition=condition+" and a.division = '"+division+"'";
        }
        if(StringUtils.isNotBlank(department)) {
            condition=condition+" and a.department = '"+department+"'";
        }
        if(StringUtils.isNotBlank(entityItem)) {
            condition=condition+" and a.ENTITY_ITEM = '"+entityItem+"'";
        }
        if(StringUtils.isNotBlank(company)) {
            condition=condition+" and a.company = '"+company+"'";
        }
        json.put("[SQL3]", condition);
        if (StringUtils.isEmpty(dataowner))
            result = vformInterface.multiForMsQueryBySql("SBMXT002", json.toJSONString(), pageNo, pageSize,"N","N","");
        else
            result = vformInterface.multiForMsQueryBySql("SBMXT002", json.toJSONString(), pageNo, pageSize,"Y","Y",dataowner);
        return result;
    }

    @Override
    public Result tfSelPersonList(String po_no, String division, String department, String entityItem, String company, String pageNo, String pageSize, String dataowner) throws Exception {
        Result<Object> result = new Result<Object>();
        JSONObject json = new JSONObject();
        json.put("[SQL1]", "");
        json.put("[SQL2]", "");
        String condition = "";
        if(StringUtils.isNotBlank(division)) {
            condition=condition+" and a.division = '"+division+"'";
        }
        if(StringUtils.isNotBlank(department)) {
            condition=condition+" and a.department = '"+department+"'";
        }
        if(StringUtils.isNotBlank(entityItem)) {
            condition=condition+" and a.ENTITY_ITEM = '"+entityItem+"'";
        }
        if(StringUtils.isNotBlank(company)) {
            condition=condition+" and a.company = '"+company+"'";
        }
        json.put("[SQL3]", condition);
        if (StringUtils.isEmpty(dataowner))
            result = vformInterface.multiForMsQueryBySql("SBMXT002", json.toJSONString(), pageNo, pageSize,"N","N","");
        else
            result = vformInterface.multiForMsQueryBySql("SBMXT002", json.toJSONString(), pageNo, pageSize,"Y","Y",dataowner);
        return result;
    }

    @Override
    public Map<String, String> addPersonInfo(Person person) {
        Map<String, String> map = new HashMap<String, String>();
        //对象转json字符串, 字段驼峰转下划线大写字段
        JSONObject json = VformBeanUtil.entityToJsonString(person);
        if (json.isEmpty()) {
            map.put("code", "-2");
            map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
            return map;
        }
        //vform同步插入
        Result result = vformInterface.insert("D4012", json);
//		System.out.println("vform增加返回结果   "+result);
        if (result == null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        if (resData.equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        JSONObject reJson = JSONObject.parseObject(resData);
        if (reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        } else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
        }
        return map;
    }

    @Override
    public Map<String, String> updatePersonInfo(Person person) {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("ENTITY_ITEM", person.getEntityItem());
        JSONObject pajson = VformBeanUtil.entityToJsonStringByUpdate(person);
        pajson.remove("ENTITY_ITEM");

        //vform同步更新
        Result result = vformInterface.update("D4012", keyjson.toString(), pajson.toString(), null, null, null);
        if (result == null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if (reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        } else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }
        return map;
    }

    @Override
    public Map<String, String> deletePersonInfo(String entityItem) {
        Map<String,String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("ENTITY_ITEM", entityItem);
        //vform同步删除
        Result result = vformInterface.delete("D4012", keyjson.toJSONString());
        if(result==null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        }else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
        }
        return map;
    }
}
